<template>
	<view class="container">
		<view class="avatar">
			<!-- 上传组件 -->
			<view style="text-align: center;padding-top: 15px;">品牌系列图片</view>
			<u-upload ref="img_logo" name="img_logo" :maxCount="1" :fileList="filelist" width="100" height="100"
				@delete="del" @afterRead="read" />
		</view>

		<view class="secetion">
			<u-form labelPosition="left" labelWidth="60" :model="prolist" :rules="rules" ref="product">


				<u-form-item label="商品分类" prop="store" borderBottom @click="showtype = true">
					<u-input v-model="prolist.typename_text" disabled inputAlign="center" disabledColor="#ffffff"
					  placeholder="请选择商品所属分类" border="none"></u-input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				  </u-form-item>
				<!-- 名称 -->
				<u-form-item label="名称" prop="name">
					<u-input v-model="prolist.name" placeholder="请输入系列名称" />
				</u-form-item>
				

				<u-popup :show="showtype" @close="closeprotype" @open="openprotype">
					<view>
						<view style="text-align: center;margin-top:15px;font-weight: bold;">请选择商品分类</view>

					  <u-list height="560px">
						<u-list-item v-for="(item, index) in protypedata" :key="index">
						  <view class="branktypename" @click="protypeSelect(item)">
							<u-cell :title="item.name" >
		  
							</u-cell>
						  </view>
						</u-list-item>
					  </u-list>
					</view>
				  </u-popup>




				<!-- 提交按钮 -->
				<u-button text="添加品牌系列" icon="pushpin" size="small" type="primary" @click="product" />
			</u-form>

		</view>
		<!-- 消息提醒 -->
		<u-toast ref="notice"></u-toast>
	</view>
</template>
  
<script>
export default {
	onLoad(options) {
		this.auth = this.$u.auth.check();

		this.brand_id = options.brand_id ? options.brand_id : 0
		this.ptypeid = options.ptypeid ? options.ptypeid : 0
		this.getproducttype()

	},

	onReady() {

		this.$refs.product.setRules(this.rules)


	},
	created() {

		this.timer = setInterval(() => {

			let a = this.$u.count.check()

		}, 15000);

	},
	beforeDestroy() {
		clearInterval(this.timer)
	},
	data() {
		return {
			showtype:false,
			ptypeid: 0,
			protypedata: [],
			brand_id: 0,
			filelist: [],
			newthumbArr:[],
			auth: {},
			stoid: null,
			img_logo: "",
			piclist: [],
			name: "",
			prolist: {
				name: "",
				ptypeid:"",
				thumb: ""
			},
			showbrank: false,
			rules: {
				content: {
					required: true,
					message: "不低于3个字",
					trigger: ["change"],
				},

				name: {
					required: true,
					message: "请填写品牌系列名称",
					trigger: ["blur", "change"],
				},
				img_logo: {
					required: true,
					message: "请填上传品牌系列图片",
					trigger: ["blur", "change"],
				},

			},

		};
	},

	methods: {

		closeprotype() {
			this.protype = false
		},
		openprotype() {
			this.protype = true
		},
		protypeSelect(e) {
			this.prolist.typename_text = e.name,
				this.prolist.ptypeid = e.id;
			this.showtype = false
		},

		async getproducttype() {
			let data = {
				adminid: this.auth.id,
			};

			let result = await this.$u.api.product.getprotypedata(data);

			if (result.code) {
				this.protypedata = result.data;
			}
		},
		async del(e) {

		
					let data = {
						img_logo: e.file.url
					}
					var result = await this.$u.api.product.delbranktypethumb(data)

					if (result.code) {
						this.$refs.notice.show({
							type: "success",
							message: result.msg,
							duration: 1000,
							complete: () => {
								// 还剩余一张的时候清空
								
									this.filelist = []

									this.prolist.thumb = ""
								


							}
						});
						return false;
					}




		

		},
		async read(event) {

			//组装上传数据
			var data = {
				filePath: event.file.thumb,
				name: "product_img",
				header: {},

			};

			//调用接口
			var result = await this.$u.api.product.thumbs(data);

			if (result.code) {

				// 合并对象
				this.filelist = this.filelist.concat({ url: result.data })

				this.newthumbArr = this.newthumbArr.concat(result.data)







				// 数组转化成字符串
				this.prolist.thumb = result.data

			} else {
				this.$refs.notice.show({
					type: "error",
					message: result.msg,
				});
				return false;
			}
		},


		product() {
			this.$refs.product.validate().then(async () => {

				var data = {
					adminid: this.auth.id,
					name: this.prolist.name,
					brand_id: this.brand_id,
					ptypeid: this.prolist.ptypeid,
					thumb: this.prolist.thumb,
					newthumbArr: this.newthumbArr

				};


				let result = await this.$u.api.product.branktypeadd(data);

				if (result.code) {
					this.$refs.notice.show({
						type: "success",
						message: result.msg,
						duration: 1000,
						complete: () => {
							//返回上一个界面
							uni.$u.route({
								type: "reLaunch",//关闭所有页面跳到某个页面
								url: "/pagesC/product/brank/type/index",
								params: {
									brand_id: this.brand_id
								}
							})

						}
					});
				} else {
					this.$refs.notice.show({
						type: "danger",
						message: result.msg,
						duration: 2000,

					});
				}
			})
				.catch(() => {
					uni.$u.toast("效验失败");
				});



		},



	},
};
</script>
  
<style >
.secetion {
	padding: 20px;
}

.container {
	height: 100%;
	margin: 0 50rpx;
	box-sizing: border-box;
}

.u-avatar {
	margin: 80rpx auto 40rpx;
}

.vedio {
	text-align: center;
}

.u-vedio {
	margin: 80rpx auto 40rpx;
}

.xcx {
	width: 200rpx;
	height: 200rpx;
	margin: 80rpx auto 40rpx;
	border-radius: 100%;
	overflow: hidden;
}

.nickname {
	font-size: 14px;
	text-align: center;
}

.title {
	text-align: center;
	margin: 40rpx 0;
	font-size: 14rpx;
}

.avatar .u-upload {
	justify-content: center;
	align-items: center;
	margin: 3em auto;
}

.vedio .u-upload {
	justify-content: center;
	align-items: center;
	margin: 0.3em auto;
}

.u-form-item__body__left__content__label.data-v-5e7216f1 {
	width: 75px;
}

.avatar .u-upload {
	justify-content: center;
	align-items: center;
	margin: 3em auto;
}
.branktypename{
	text-align: center;
}
</style>
  